<?php
/**
 * Created by PhpStorm.
 * User: caoguangyang
 * Date: 2023-06-17
 * Time: 9:20
 */
namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Models\User\Address;
use App\Services\User\AddressServices;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class AddressController extends WxController
{
    /**
     * 获取用户地址
     * @return \Illuminate\Http\JsonResponse
     */
    public function list() {
        $list = AddressServices::getInstance()->getAddressListByUserId($this->user()->id);

        return $this->successPaginate($list);
//        $list = $list->map(
//            function (Address $address) {
//                $address = $address->toArray();
//                $item = [];
//                foreach ($address as $key => $value) {
//                    $key = lcfirst(Str::studly($key));
//                    $item[$key] = $value;
//                }
//                return $item;
//            }
//        );
//        return $this->success([
//            "total" => $list->count(),
//            "page" => 1,
//            "list" => $list->toArray(),
//            'pages' => 1
//        ]);
    }

    /**
     * 地址详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request) {
        // 接收数据
        $id = $request->input("id", 0);

        // 判断
        if (empty($id) && !is_numeric($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $res = AddressServices::getInstance()->getAddress($this->user()->id, $id);
        return $this->success($res);
    }

    /**
     * 删除地址
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request) {
        // 接收数据
        $id = $request->input("id", 0);

        // 判断
        if (empty($id) && !is_numeric($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $res = AddressServices::getInstance()->delete($this->user()->id, $id);
        return $this->failOrSuccess($res);
    }

    // 添加地址 TODO
    public function add(Request $request) {

    }

    // 更新地址 TODO
    public function save(Request $request) {

    }
}
